#include"Game.h"

int selectRole = EMPTY;                   //鼠标的选择角色

int map[5][4] = {
	MA_CAO,     CAO_CAO,CAO_CAO,  ZHAO_YUN,
	MA_CAO,     CAO_CAO,CAO_CAO,  ZHAO_YUN,
	HUANG_ZHONG,GUAN_YU,GUAN_YU,  ZHANG_FEI,
	HUANG_ZHONG,  ZU1,    ZU2,    ZHANG_FEI,
	  ZU3,        EMPTY,  EMPTY,     ZU4
};

//定义一个结构体，表示方向

struct direction
{
	int row_off;
	int col_off;
}MOVE_UP = { -1,0 }, MOVE_DOWN = { 1,0 }, MOVE_LEFT = { 0,-1 }, MOVE_RIGHT = { 0,1 };


struct position
{
	int row;
	int col;
};

//初始化
void Game::gameInit()
{
	//窗口
	initgraph(507, 855);//宽度和高度	
	loadimage(&imgBg, "img/bg.jpg");           //把游戏背景加载到内存变量中去
	loadimage(&menuitf, "img/菜单.jpg");       //把菜单背景加载到内存变量中去

	char name[64];
	for (int i = 1; i <= 10; i++)
	{
		sprintf_s(name, sizeof(name), "img/%d.0.jpg", i);
		loadimage(&imgs[i][0], name);           //把人物头像加载到内存变量中去

		sprintf_s(name, sizeof(name), "img/%d.1.jpg", i);
		loadimage(&imgs[i][1], name);            //把人物头像加载到内存变量中去
	}
}
//菜单
void Game::menu()
{
	MOUSEMSG mmsg;
	putimage(0, 0, &menuitf);
	while (true)
	{
		while (MouseHit())
		{
			mmsg = GetMouseMsg();
			if (mmsg.uMsg == WM_LBUTTONDOWN)
			{
				if (mmsg.x >= 150 && mmsg.x <= 350)
				{
					if (mmsg.y >= 125 && mmsg.y <= 175)
					{
						game();						
					}
				}
			}
		}
	}
}

//加载游戏窗口
void Game::updateWindow()
{
	BeginBatchDraw();

	//打印背景图片
	putimage(0, 0, &imgBg);

	for (int i = 1; i <= 10; i++)
	{
		for (int row = 0; row < 5; row++)
		{
			for (int col = 0; col < 4; col++)
			{
				if (map[row][col] == i)
				{
					int x = 5 + col * (120 + 5);
					int y = 5 + row * (120 + 5);
					if (selectRole == i)
					{
						putimage(x, y, &imgs[i][1]);
					}
					else
					{
						putimage(x, y, &imgs[i][0]);
					}
					goto NEXT;
				}
			}
		}
	NEXT:
		continue;
	}
	EndBatchDraw();
}

//判断能否移动
bool Game::CanMove(struct direction direction)
{
	for (int row = 0; row < 5; row++)
	{
		for (int col = 0; col < 4; col++)
		{
			if (map[row][col] == selectRole)
			{
				if (map[row + direction.row_off][col + direction.col_off] != EMPTY &&
					map[row + direction.row_off][col + direction.col_off] != selectRole)
				{
					return false;
				}
			}
		}
	}
	return true;
}


//移动
void Game::move(struct direction direction)
{
	//处理不能动.判断能不能动
	if (CanMove(direction) == false)
	{
		selectRole = EMPTY;
		return;
	}
	//定义一个数组，用来保存这个人物包含的所有方块
	struct position blocks[4];
	int count = 0;

	for (int row = 0; row < 5; row++)
	{
		for (int col = 0; col < 4; col++)
		{
			if (map[row][col] == selectRole)
			{
				map[row][col] = EMPTY;//擦除原来位置的值
				blocks[count].row = row;
				blocks[count].col = col;
				count++;
			}
		}
	}

	for (int i = 0; i < count; i++)
	{
		int row = blocks[i].row + direction.row_off;
		int col = blocks[i].col + direction.col_off;
		map[row][col] = selectRole;
	}

	selectRole = EMPTY;
}


//获取鼠标信息
void Game::UserClick()
{
	msg = getmessage();                       //获取消息
	if (msg.message == WM_LBUTTONDOWN)        //若为鼠标左键
	{
		int row = msg.y / (120 + 5);
		int col = msg.x / (120 + 5);

		if (selectRole == EMPTY)
		{
			selectRole = map[row][col];
		}
		else
		{
			//第二次选择
			if (map[row][col] != EMPTY)
			{
				selectRole = map[row][col];      //重新选择
				return;
			}

			if (row >= 1 && map[row - 1][col] == selectRole)
			{
				move(MOVE_DOWN);               //向下移动函数
			}

			if (row + 1 < 5 && map[row + 1][col] == selectRole)
			{
				move(MOVE_UP);                //向上移动函数
			}

			if (col + 1 < 4 && map[row][col + 1] == selectRole)
			{
				move(MOVE_LEFT);              //向左移动函数
			}

			if (col - 1 >= 0 && map[row][col - 1] == selectRole)
			{
				move(MOVE_RIGHT);              //向右移动函数
			}
		}
	}
}

void Game::over()                      // 结束判断函数
{
	if (map[4][2] == 9 || map[4][1] == 1)
	{
		MessageBox(hOut, _T("成功啦，点击确定即刷新"), _T("来自元清的提醒"), MB_OK);
		num_over = 1;
	}
}

void Game::game()
{
	while (num_over == 0)
	{
		UserClick();                  //用户点击
		updateWindow();
		over();                       //判断输赢
	}

}

// 主进程
void wh::carry()
{
    
	gameInit();                       //游戏初始化
	menu();                           //菜单
}